using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI_InventorySlot : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField] private Image _inventorySlotImage;
    [SerializeField] private Image _inventorySlotBorderImage;
    private UI_BackPack _inventoryUI;
    public ItemDataSO CurrentItemDataSO { get; private set; }
    private bool _isClick;
    private void Awake()
    {
        _inventoryUI = GetComponentInParent<UI_BackPack>();
    }
    public void SetupInventorySlot(ItemDataSO itemDataSO)
    {
        Debug.Log(itemDataSO);
        CurrentItemDataSO = itemDataSO;
        _inventorySlotImage.sprite = itemDataSO.itemIcon;

        _inventorySlotBorderImage.enabled = false;
        _isClick = false;
        _inventorySlotImage.color = Color.white;
    }
    public void HiddleInventorySlot()
    {
        _inventorySlotImage.color = Color.clear;
        _inventorySlotBorderImage.enabled = false;
        CurrentItemDataSO = null;
    }
    public void ResetClickState()
    {
        _inventorySlotBorderImage.enabled = false;
        _isClick = false;
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (_isClick || CurrentItemDataSO == null)
            return;
        
        _inventorySlotBorderImage.enabled = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (_isClick || CurrentItemDataSO == null)
            return;
        
        _inventorySlotBorderImage.enabled = false;
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        if (CurrentItemDataSO == null)
            return;
        _isClick = !_isClick;
        _inventorySlotBorderImage.enabled = _isClick;

        if (CurrentItemDataSO.itemType == ItemType.Material)
            return;
            
        _inventoryUI.SetEquipmentDataSO(CurrentItemDataSO);
    }    
}
